/**
 * Copyright (C) 2021 - 2031 O-Cubes Co., Ltd.
 */

/****************************************************************
 *  @file    spi_dma_test.c
 *  @brief   spi dma testcase source file
 *  @version v1.0
 *  @date    03. Apr. 2023
 ****************************************************************/

#include "hal_api.h"
#include "printf.h"
#include "common.h"

#define DMA_TX_DMA_CHAN            0
#define DMA_RX_DMA_CHAN            1
#define BUF_LEN                    1024

#define SPI_FLASH_READ_CMD         0x03
#define SPI_FLASH_ADDR             0x41800
#define SPI_CS_NO                  0

static __align(64) uint8_t spi_dma_rbuf[BUF_LEN];
static __align(64) uint8_t spi_dma_wbuf[BUF_LEN];

static void spi_flash_dma_read_data(Spi_Cfg_t_Ptr ptr)
{
	spi_dma_wbuf[0] = SPI_FLASH_READ_CMD;
	spi_dma_wbuf[1] = (SPI_FLASH_ADDR >> 16) & 0xff;
	spi_dma_wbuf[2] = (SPI_FLASH_ADDR >> 8) & 0xff;
	spi_dma_wbuf[3] = SPI_FLASH_ADDR & 0xff;
	Hal_Spi_Transfer(ptr, spi_dma_wbuf, 4, spi_dma_rbuf, 1020);
}


int32_t test_spi_dma(void)
{
	uint32_t i;
	int32_t ret = 0;
	Spi_Cfg_t spi = {SPI_ID_3, SPI_CS_NO};
	printf("\r\n[SPI DMA]: TEST START\r\n");
	ret = Hal_Spi_Request_Dma_Chan(&spi, DMA_TX_DMA_CHAN, DMA_RX_DMA_CHAN);
	if (ret) {
		printf("[SPI DMA]: spi request dma channel failed\r\n");
	}
	spi_flash_dma_read_data(&spi);
	for (i = 16 ; i < 1024; i += 4) {
		printf("[SPI DMA]: the read dma data is %d\r\n", spi_dma_rbuf[i]);
	}
	printf("\r\n[SPI DMA]: TEST END\r\n");
	return 0;
}


